import {getDataJson, pollingRefresh} from '@/utils/refreshCptData';
import {funEval} from "@/utils/FunUtil";

export default {
  props: {
    option: Object
  },
  data() {
    return {
      cptData: {},
      uuid: null
    }
  },
  mounted() {
    this.uuid = require('uuid').v1();
    this.refreshCptData();

    if(this.option.events) {
      // 组件加载完
      if(this.option.events.onReady) {
        const fn = funEval(this.option.events.onReady)
        fn(this)
      }
    }
  },
  methods: {
    refreshCptData(otherParam) {
      this.otherParam = otherParam
      pollingRefresh(this.uuid, this.option.cptDataForm, this.loadData)
    },
    loadData() {
      getDataJson(this.option.cptDataForm, this.otherParam).then(res => {
        this.cptData = res;

        this.loadDataOk && this.loadDataOk()
      });
    },
    clickHandler() {
      this.$emit("clickHandler", this.option.interaction, 'default', this.cptData.value)
    },
    setFormatter(obj) {
      if(obj.formatterFn && obj.formatterFn.trim().length > 0) {
        obj.formatter = funEval(obj.formatterFn)
      } else if(!obj.formatter) {
        obj.formatter = null
      }
    }
  }
}
